<?php
include_once("../DAO/FeedbackDAO.php");
include_once("../Utility/WebUtilities.php");
include_once("../DAO/UserDAO.php");
include_once("../DAO/QuestionDAO.php");
include_once("../Utility/WebSession.php");

if(WebSession::CurrentUserNoReturn() == null || WebSession::CurrentUserNoReturn()->Role != WebUtilities::GetAdminRoleId())
{
	WebUtilities::Redirect("index.php");
}

$feedbackDAO = new FeedbackDAO();
$questionDAO = new QuestionDAO();
$comand = WebUtilities::GetQueryString("hdCommand", "");
if($comand == 0) // change status
{
	$ids = split(",", WebUtilities::GetQueryString("hdSelected", ""));
	for($i = 0; $i < count($ids); $i++)
	{
		$feedbackDAO->UpdateStatus($ids[$i], 1);
	}
}
else if($comand == 1) // delete
{
	$ids = split(",", WebUtilities::GetQueryString("hdSelected", ""));
	for($i = 0; $i < count($ids); $i++)
	{
		$feedbackDAO->DeleteFeedback($ids[$i]);
	}
}

$status = WebUtilities::GetQueryString("status", -1);
$pageSize = WebUtilities::GetPageSize();
$page = WebUtilities::GetQueryString("page", 1);
$totalPage = 0;

$feedbackList = $feedbackDAO->GetFeedbacks($page, $pageSize, $totalPage, $status);
$feedbackStr = "";
if(count($feedbackList) == 0)
{
	$feedbackStr = "<tr><td colspan='7'>No items</td></tr>";
}
else
{
	$userDAO = new UserDAO();
	for($i = 0; $i < count($feedbackList); $i++)
	{
		$item = $feedbackList[$i];
		$question = $questionDAO->GetQuestionById($item->Question);
		$strEdit = "<a onclick=\"window.open('EditQuestion.php?id=" . $question->Id . "')\">Edit question</a>";
		if($question->Paragraph != null)
		{
			$strEdit = $strEdit . "<a onclick=\"window.open('EditParagraph.php?id=" . $question->Paragraph . "')\">Edit paragraph</a>";
		}
		$user = $userDAO->GetUserById($item->CreatedBy);
		$feedbackStr = $feedbackStr . "<tr><td class='action'><input type='checkbox' onclick='checkProcess.checkItem(this.checked)' class='checkItems' name='checkItems" . $i . "' value='" . $item->Id . "'/></td>"
									. "<td>" . $item->Id . "</td>"
									. "<td>" . ($user == null ? "" : $user->Username) . "</td>"
									. "<td>" . $item->CreationOn . "</td>"
									. "<td><a onclick=\"ShowDescription('" . $item->Description . "')\" >View Description</a></td>"
									. "<td>" . $strEdit . "</td>"
									. "<td>" . ($item->IsFixed == 1 ? "Yes" : "No") . "</td></tr>";
									
									
	}
}

$submitUrl = "FeedbackManagement.php?page=" . $page . "&status=" . $status;
$pageUrlFirst = "<a href='FeedbackManagement.php?status=" . $status . "&page=1'>First</a>";
$pageUrlLast = "<a href='FeedbackManagement.php?status=" . $status . "&page=" . $totalPage ."'>Last</a>";
$pageUrlNext = "<a href='FeedbackManagement.php?status=" . $status . "&page=" . ($page + 1) ."'>Next ></a>";
$pageUrlPrev = "<a href='FeedbackManagement.php?status=" . $status . "&page=" . ($page - 1) ."'>< Prev</a>";
if($page <= 1)
{
	$pageUrlFirst = "First";
	$pageUrlPrev = "< Prev";
}
if($page >= $totalPage)
{
	$pageUrlLast = "Last";
	$pageUrlNext = "Next >";
}
$mainContent = "<form enctype='multipart/form-data' action='" . $submitUrl ."' method='POST'>
					<div class='oneLine'>
						<div class='left'>Status</div>
						<select class='right' id='cbStatus'>
							<option value='-1' " . ($status == -1 ? "selected='true'" : "") . ">All</option>
							<option value='0' " . ($status == 0 ? "selected='true'" : "") . ">Not Fixed</option>
							<option value='1' " . ($status == 1 ? "selected='true'" : "") . ">Fixed</option>
						</select>
						<input value='Search' type='button' onclick='SearchFeedback()'/>
					</div>
					<br/>
					<table class='manager' id='tableAnswers'>
						<thead>
							<tr>
								<th><input type='checkbox' id='checkAllItem' name='checkAllItem' onclick='checkProcess.checkAll(this.checked)' /></th>
								<th>Feedback Id</th>
								<th>Created By</th>
								<th>Creation On</th>
								<th>Description</th>
								<th>Edit</th>
								<th>Fixed</th>
							</tr>
						</thead>
						<tbody class='tableBody'>"
							. $feedbackStr . 
						"</tbody>
					</table>" .
				"<div class='paging'>
					Page " . ($page >= $totalPage ? $totalPage : $page) . " of " . $totalPage . " " . 
					$pageUrlFirst . " | " . $pageUrlPrev . " | " . $pageUrlNext . " | " . $pageUrlLast . 
				"</div><div class='paging'>
					<div>
						<input type='button' value='Delete' name='btDelete' onclick=\"DeleteItems('feedbacks')\"/>
						<input type='button' value='Fixed' onclick='ChangeStatusFeedback()'/>
					</div>
				</div>
				<input type='hidden' id='hdSelected' name='hdSelected' value='' />
				<input type='hidden' id='hdCommand' name='hdCommand' value='' />
				</form>
				<div id='popupPanel' class='popupPanel'>
					<a href='#' onclick='ClosePopupPanel(); return false;'>Close</a>
					<div id='panelContent' style='padding:10px 10px 10px 10px;overflow:auto;'></div>
				</div>";
$scriptContent = "checkProcess.totalItems = " . count($feedbackList) . ";" . 
	"checkProcess.checkAllItems = document.getElementById('checkAllItem');";
$pageIndex = 6;
include("MasterPage.php");
?>